package com.p2p.base.util.sms;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HttpContext;

/**
 * @author dan.shan
 * @since 2013-08-12 3:23 PM
 */
public class LuoSiMaoSMS {

	public static String URL = "https://sms-api.luosimao.com/v1/send.json";
	public static String STATUS_URL = "https://sms-api.luosimao.com/v1/status.json";
	public static String KEY = "444b136a66d94626d11e73d759033e15";
	public static String SIGN = "【一起好】";

	public static void main(String[] args) throws Exception {
		LuoSiMaoSMS api = new LuoSiMaoSMS();
		String mobile = "18971326300";
		String text = "测试短信2";
		api.testStatus(mobile, text);
		//api.sendSms(mobile, text);
	}

	public static String sendSms(String mobile, String text) throws Exception {

		DefaultHttpClient client = new DefaultHttpClient();

		client.addRequestInterceptor(new HttpRequestInterceptor() {
			@Override
			public void process(HttpRequest request, HttpContext context)
					throws HttpException, IOException {
				request.addHeader("Accept-Encoding", "gzip");
				request.addHeader(
						"Authorization",
						"Basic "
								+ new Base64()
										.encodeToString(("api:key-" + KEY)
												.getBytes("utf-8")));
			}
		});

		client.getParams().setIntParameter(
				CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
		client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT,
				30000);

		HttpPost request = new HttpPost(URL);

		ByteArrayOutputStream bos = null;
		InputStream bis = null;
		byte[] buf = new byte[10240];

		String content = null;
		try {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("mobile", mobile));
			params.add(new BasicNameValuePair("message", text + SIGN));
			request.setEntity(new UrlEncodedFormEntity(params, "utf-8"));

			HttpResponse response = client.execute(request);

			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				bis = response.getEntity().getContent();
				Header[] gzip = response.getHeaders("Content-Encoding");

				bos = new ByteArrayOutputStream();
				int count;
				while ((count = bis.read(buf)) != -1) {
					bos.write(buf, 0, count);
				}
				bis.close();

				if (gzip.length > 0
						&& gzip[0].getValue().equalsIgnoreCase("gzip")) {
					GZIPInputStream gzin = new GZIPInputStream(
							new ByteArrayInputStream(bos.toByteArray()));
					StringBuffer sb = new StringBuffer();
					int size;
					while ((size = gzin.read(buf)) != -1) {
						sb.append(new String(buf, 0, size, "utf-8"));
					}
					gzin.close();
					bos.close();

					content = sb.toString();
				} else {
					content = bos.toString();
				}

				System.out.println(content);
			} else {
				System.out.println("error code is "
						+ response.getStatusLine().getStatusCode());
			}
			return content;

		} finally {
			if (bis != null) {
				try {
					bis.close();// 最后要关闭BufferedReader
				} catch (Exception e) {
				}
			}
		}
	}

	private String testStatus(String mobile, String text) throws Exception {

		DefaultHttpClient client = new DefaultHttpClient();

		client.addRequestInterceptor(new HttpRequestInterceptor() {
			@Override
			public void process(HttpRequest request, HttpContext context)
					throws HttpException, IOException {
				request.addHeader("Accept-Encoding", "gzip");
				request.addHeader(
						"Authorization",
						"Basic "
								+ new Base64().encodeToString(("api:" + KEY)
										.getBytes("utf-8")));
			}
		});

		client.getParams().setIntParameter(
				CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
		client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT,
				30000);

		HttpGet request = new HttpGet(STATUS_URL);

		ByteArrayOutputStream bos = null;
		InputStream bis = null;
		byte[] buf = new byte[10240];

		String content = null;
		try {
			
			HttpResponse response = client.execute(request);

			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				bis = response.getEntity().getContent();
				Header[] gzip = response.getHeaders("Content-Encoding");

				bos = new ByteArrayOutputStream();
				int count;
				while ((count = bis.read(buf)) != -1) {
					bos.write(buf, 0, count);
				}
				bis.close();

				if (gzip.length > 0
						&& gzip[0].getValue().equalsIgnoreCase("gzip")) {
					GZIPInputStream gzin = new GZIPInputStream(
							new ByteArrayInputStream(bos.toByteArray()));
					StringBuffer sb = new StringBuffer();
					int size;
					while ((size = gzin.read(buf)) != -1) {
						sb.append(new String(buf, 0, size, "utf-8"));
					}
					gzin.close();
					bos.close();

					content = sb.toString();
				} else {
					content = bos.toString();
				}

				System.out.println(content);
			} else {
				System.out.println("error code is "
						+ response.getStatusLine().getStatusCode());
			}
			return content;

		} finally {
			if (bis != null) {
				try {
					bis.close();// 最后要关闭BufferedReader
				} catch (Exception e) {
				}
			}
		}
	}
}